package algebra.op;

import java.util.HashSet;
import java.util.Set;

import option.QueryKeywords;

import kb.DescriptionLogicsKB;
//import entities.concept.AndConcept;
import entities.concept.Concept;
import entities.role.Role;

/**
 * A concept query returns a:C s.t. a is in K; Note: a special C query is \top
 * query, which forms the primary index.
 * 
 * @author j55wu
 * 
 */
public final class ConstantOP extends AlgebraicOP {
	private final Concept c;
	private final DescriptionLogicsKB kb;

	public ConstantOP(Concept c) {
		this.c = c;
		this.kb = null;
	}

	public Concept getQueryCocnept() {
		return this.c;
	}

	// public Concept addQueryConcept(Concept d){
	// this.c = new AndConcept(this.c, d);
	// return this.c;
	// }
	//
	// public Concept changeQueryConcept(Concept d){
	// this.c = d;
	// return d;
	// }

	@Override
	public boolean isPure() {
		return false;
	}

	@Override
	public String toString() {
		return "(" + this.c.toString() + ")";
	}

	@Override
	public String toSerializedString() {
		return " (" + QueryKeywords.QCON + " " + this.c.toSerializedString()
				+ ") ";
	}

	@Override
	public DescriptionLogicsKB getPlanEvalKB() {
		// no kb needed
		return this.kb;
	}

	@Override
	public Set<Role> extractFeatures() {
		return this.c.extractFeatures();
	}

	@Override
	public Set<String> extractConstants() {
		Set<String> ks = new HashSet<String>();
		ks.addAll(this.c.extractConstants());
		return ks;
	}

	@Override
	public boolean isLeaf() {
		return true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((c == null) ? 0 : c.hashCode());
		result = prime * result + ((kb == null) ? 0 : kb.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof ConstantOP))
			return false;
		ConstantOP other = (ConstantOP) obj;
		if (c == null) {
			if (other.c != null)
				return false;
		} else if (!c.equals(other.c))
			return false;
		if (kb == null) {
			if (other.kb != null)
				return false;
		} else if (!kb.equals(other.kb))
			return false;
		return true;
	}

	@Override
	public AlgebraicOP getKFreeVersion() {
		return this;
	}

}
